package managesystem;

import managesystem.help.Help;
import managesystem.item.Item;
import managesystem.login.UserAccount;
import managesystem.manager.Manager;
import managesystem.poverty.Poverty;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

/**
 * @author The setting sun
 * @version 1.0
 * @title: JDBCUtils
 * @description: 操作数据库的工具类
 * @date 2021/10/30 17:57
 */
public class JDBCUtils {
    /**
     * @author: The setting sun
     * @param: []
     * @return: java.sql.Connection
     * @description: 获取数据库的连接
     * @date: 2021/10/30 18:02
     */
    public static Connection getConnection() {
        try {
            String url = "jdbc:mysql://localhost:3306/mngsystem?useUnicode=true&characterEncoding=utf8";
            String user = "root";
            String password = "1509982360";

            Connection connection = DriverManager.getConnection(url, user, password);

            return connection;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    /**
     * @author: The setting sun
     * @param: [connection, sql, args]
     * @return: java.util.ArrayList<managesystem.login.UserAccount>
     * @description: 匹配用户的账号
     * @date: 2021/12/6 11:13
     */
    public static ArrayList<UserAccount> verificationUserAccount(Connection connection, String sql, Object... args) {
        ArrayList<UserAccount> list = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            list = new ArrayList<>();
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            while (rs.next()) {
                UserAccount account = new UserAccount();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = UserAccount.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(account, value);
                }

                list.add(account);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * @author: The setting sun
     * @param: [connection, sql, urgs]
     * @return: void
     * @description: 用户注册，添加用户账号
     * @date: 2021/12/6 11:14
     */
    public static void addUser(Connection connection, String sql, Object... urgs) {
        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < urgs.length; i++) {
                ps.setObject(i + 1, urgs[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 在数据库中添加一个贫困用户信息
     * @date: 2021/12/6 11:42
     */
    public static void addPoverty(String sql, Object... args) {
        Connection connection = getConnection();
        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 在数据库中添加一个帮扶人员信息
     * @date: 2021/12/8 20:10
     */
    public static void addHelp(String sql, Object... args) {
        Connection connection = getConnection();
        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 在数据库中添加一个贫困项目信息
     * @date: 2021/12/8 20:10
     */
    public static void addItem(String sql, Object... args) {
        Connection connection = getConnection();
        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: java.util.ArrayList<managesystem.item.Item>
     * @description: 获得数据库中所有的贫困项目信息
     * @date: 2021/12/6 11:16
     */
    public static ArrayList<Item> getItemAllInfo(String sql, Object... args) {
        Connection connection = getConnection();
        ArrayList<Item> list = new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            list = new ArrayList<>();
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            while (rs.next()) {
                Item item = new Item();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = Item.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(item, value);
                }

                list.add(item);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: managesystem.poverty.Poverty
     * @description: 获得数据库中单个贫困项目信息
     * @date: 2021/12/7 22:28
     */
    public static Item getItemOneInfo(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            connection = JDBCUtils.getConnection();

            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                Item item = new Item();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);

                    Field field = Item.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(item, value);
                }

                return item;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: java.util.ArrayList<managesystem.poverty.Poverty>
     * @description: 获得数据库中所有的贫困用户信息
     * @date: 2021/12/6 11:15
     */
    public static ArrayList<Poverty> getPovertyAllInfo(String sql, Object... args) {
        Connection connection = getConnection();
        ArrayList<Poverty> list = new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            list = new ArrayList<>();
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            while (rs.next()) {
                Poverty poverty = new Poverty();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = Poverty.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(poverty, value);
                }

                list.add(poverty);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: managesystem.poverty.Poverty
     * @description: 获得数据库中单个贫困用户信息
     * @date: 2021/12/6 11:17
     */
    public static Poverty getPovertyOneInfo(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            connection = JDBCUtils.getConnection();

            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                Poverty poverty = new Poverty();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);

                    Field field = Poverty.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(poverty, value);
                }

                return poverty;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: java.util.ArrayList<managesystem.help.Help>
     * @description: 获得数据库中所有的帮扶人员信息
     * @date: 2021/12/6 11:16
     */
    public static ArrayList<Help> getHelpAllInfo(String sql, Object... args) {
        Connection connection = getConnection();
        ArrayList<Help> list = new ArrayList();
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            list = new ArrayList<>();
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            while (rs.next()) {
                Help help = new Help();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);
                    Field field = Help.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(help, value);
                }

                list.add(help);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        closeResources(connection, ps, rs);

        return list;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: managesystem.help.Help
     * @description: 获得数据库中单个帮扶用户信息
     * @date: 2021/12/7 14:35
     */
    public static Help getHelpOneInfo(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            connection = JDBCUtils.getConnection();

            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                Help help = new Help();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);

                    Field field = Help.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(help, value);
                }

                return help;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: managesystem.manager.Manager
     * @description: 获得数据库中管理员信息
     * @date: 2021/12/8 15:33
     */
    public static Manager getManagerInfo(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            connection = JDBCUtils.getConnection();

            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                Manager mng = new Manager();

                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    Object value = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);

                    Field field = Manager.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(mng, value);
                }

                return mng;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 修改贫困用户信息
     * @date: 2021/12/6 11:18
     */
    public static void UpdatePovertyInfo(String sql, Object... args) {
        Connection connection = getConnection();

        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 修改帮扶用户信息
     * @date: 2021/12/6 11:18
     */
    public static void UpdateHelpInfo(String sql, Object... args) {
        Connection connection = getConnection();

        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 修改贫困项目信息
     * @date: 2021/12/8 15:34
     */
    public static void UpdateItemInfo(String sql, Object... args) {
        Connection connection = getConnection();

        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 删除数据库中一个贫困人员
     * @date: 2021/12/8 11:34
     */
    public static void deletePoverty(String sql, Object... args) {
        Connection connection = getConnection();

        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 删除一个帮扶人员
     * @date: 2021/12/8 15:35
     */
    public static void deleteHelp(String sql, Object... args) {
        Connection connection = getConnection();

        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [sql, args]
     * @return: void
     * @description: 删除一个贫困项目
     * @date: 2021/12/8 15:35
     */
    public static void deleteItem(String sql, Object... args) {
        Connection connection = getConnection();

        PreparedStatement ps = null;

        try {
            ps = connection.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * @author: The setting sun
     * @param: [connection, ps]
     * @return: void
     * @description: 关闭资源的操作
     * @date: 2021/10/30 18:05
     */
    public static void closeResources(Connection connection, PreparedStatement ps) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * @author: The setting sun
     * @param: [connection, ps, resultSet]
     * @return: void
     * @description: 关闭资源的操作
     * @date: 2021/10/31 16:33
     */
    public static void closeResources(Connection connection, PreparedStatement ps, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}